var router = require('koa-router')();
var superagent = require('superagent-promise')(require('superagent'), Promise);
var cheerio = require('cheerio');
/*
router=new router({
    prefix:'/users'
})
*/

router.get('/', function (ctx, next) {
    function* ceshi(num){
        yield num
        console.log('aaa='+(yield "test"))
        console.log('bbb='+(yield "shanghai"))
        yield "www.hfjy.com"
        return "end"
    }
    let g=ceshi(5)
    console.log(g.next())
    console.log(g.next())
    console.log(g.next("hehe"))
    console.log(g.next(3))
    console.log(g.next(3))
  ctx.body = 'this a users response!';
});
router.get('/test/:id/:cat',async (ctx,next)=>{

    console.log(ctx.params)
    let res=await superagent.get('http://localhost:81/api/order/order.php?act=getUserNoByOrderSN&order_sn=2017020961672,2017020945619,2017020945612')
    console.log(res.text);
    let temp=JSON.parse(res.text);
    console.log(temp['data'])
    //let $ = cheerio.load(res.text);
    ctx.state={
        title:'test',
        res:JSON.stringify(temp),
        params:JSON.stringify(ctx.params)
    };
    await ctx.render('test',{})
})
router.get('/article/list',async (ctx,next)=>{
    let res=await superagent.get('http://blog-admin.54json.com/article/getlist')
    let temp=JSON.parse(res.text);
    ctx.state={
        title:'test',
        items:temp,
    };
    await ctx.render('list',{})
})
router.get('/article/detail/:id',async (ctx,next)=>{
    console.log(ctx.params.id)
    let res=await superagent.get('http://blog-admin.54json.com/article/getdetail/'+ctx.params.id)
    console.log(res.text);
    let temp=JSON.parse(res.text);
    //let $ = cheerio.load(res.text);
    ctx.state={
        title:'test',
        items:temp,
    };
    await ctx.render('detail',{})
})
router.get('/article/list-data',async (ctx,next)=>{

    console.log(ctx.params)
    let res=await superagent.get('http://blog-admin.54json.com/article/getlist')
    //console.log(res.text);
    let temp=JSON.parse(res.text);
    console.log(temp['data'])
    //let $ = cheerio.load(res.text);
    ctx.state={
        title:'test',
        res:temp,
        //data:JSON.stringify(temp),
        //params:JSON.stringify(ctx.params)
    };
    ctx.response.body=JSON.stringify(data)
    //await ctx.render('list.html',{})
})

module.exports = router;
